package com.service.impl;

import com.constant.BoxTypeConstant;
import com.dao.AdvertisementMapper;
import com.dao.GiveBackRecordMapper;
import com.dao.OrdersMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.model.GiveBackRecord;
import com.model.Orders;
import com.page.GenericPage;
import com.po.OrdersPO;
import com.result.Result;
import com.service.OrdersService;
import com.utils.CurrentLoginUserAgent;
import com.vo.CustomerVO;
import com.vo.OrderCondition;
import com.vo.OrdersQueryVO;
import com.vo.OrdersVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: chihaojie
 * @Date: 2019/5/29 16:02
 * @Version 1.0
 * @Note
 */

@Slf4j
@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private GiveBackRecordMapper giveBackRecordMapper;


    @Override
    public OrdersVO save(OrdersPO ordersPO) {
        return null;
    }

    @Override
    public Result<Orders> selectByOrderNumber(String orderNumber) {
        Orders orders = ordersMapper.selectByOrderNumber(orderNumber);
        if (orders == null) {
            return new Result<>().error("订单不存在");
        }
        return new Result<Orders>().ok(orders);
    }


    public Result<Boolean> updateLockStatus(Long orderId, int lockStatus) {
        Orders orders = new Orders();
        orders.setId(orderId);
        orders.setLockStatus(lockStatus);
        ordersMapper.updateByPrimaryKeySelective(orders);
        return new Result<>().ok();
    }


    @Override
    public Result<GenericPage<OrdersQueryVO>> findOrderByPage(OrderCondition conditionParam) {
        Result<GenericPage<OrdersQueryVO>> result = new Result<GenericPage<OrdersQueryVO>>();
        Long businessId = CurrentLoginUserAgent.getBusinessId();
        PageHelper.startPage(conditionParam.getPageNo(), conditionParam.getPageSize());
        Orders condition = new Orders();
        BeanUtils.copyProperties(conditionParam, condition);
        condition.setAgent(businessId + "");
        List<OrdersQueryVO> list = new ArrayList<>();
        PageInfo<OrdersQueryVO> pageInfo = PageHelper.startPage(conditionParam.getPageNo(), conditionParam.getPageSize()).doSelectPageInfo(() -> {
                    List<Orders> orderList = ordersMapper.selectOrderListByCondition(condition);
                    OrdersQueryVO vo = null;
                    for (Orders orders:orderList){
                        vo = new OrdersQueryVO();
                        BeanUtils.copyProperties(orders,vo);
                        if (orders.getOrderType() == BoxTypeConstant.RENT){
                            Long rentLength = Double.valueOf(Math.ceil(Double.valueOf((orders.getRentEndTime().getTime()-orders.getRentStartTime().getTime()))/1000/60/60)).longValue();
                            vo.setRentLength(rentLength);
                            //TODO  为租赁订单填充归还设备信息
                            GiveBackRecord giveBackRecord = giveBackRecordMapper.selectByOrderNum(orders.getOrderNum());
                            if(!ObjectUtils.isEmpty(giveBackRecord)){
                                vo.setGiveBack(true);
                                vo.setGiveBackDeviceId(giveBackRecord.getDeviceId());
                                vo.setGiveBackDeviceSn(giveBackRecord.getDeviceSn());
                                vo.setGiveBackTime(giveBackRecord.getUpdateTime());
                            }else    vo.setGiveBack(true);
                            list.add(vo);
                        }else{
                            list.add(vo);
                        }
                    }
                });
        GenericPage<OrdersQueryVO> genericPage = new GenericPage<OrdersQueryVO>(conditionParam.getPageSize(), pageInfo.getTotal(), list);
        return result.ok(genericPage);
    }
}
